/**
 * Copyright (C) 2010 Neofonie GmbH
 *
 * This programm is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 */
package eu.dicodeproject.analysis.xsd.topics;

import java.io.Reader;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import eu.dicodeproject.analysis.xsd.topics.Topics;

/**
 * @author messner@neofonie.de
 * 
 */
public class TopicsUtil {
  private static final JAXBContext jaxbContext;

  static {
    try {
      jaxbContext = JAXBContext.newInstance(Topics.class.getPackage().getName(), Topics.class.getClassLoader());
    } catch (JAXBException e) {
      throw new Error("JAXB error - is there a JAXB implementation on the Classpath?", e);
    }
  }

  private TopicsUtil() {
    // static only
  }

  public static void marshal(Writer writer, Topics topics) throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    marshaller.marshal(topics, writer);
  }

  public static Topics unmarshal(Reader reader) throws JAXBException {    
    return (Topics) jaxbContext.createUnmarshaller().unmarshal(reader); 
  }

}
